/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.log.log4j;

import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggerFactory;
import org.auditlogger.api.AuditLogger;
import org.auditlogger.api.AuditLoggerFactory;


/**
 * This is a reference implementation of the <code>AuditLogFactory</code> interface.
 * <p>
 * When this class is initialized, the file log4j.properties or log4j.xml file will be loaded 
 * from the classpath. 
 * 
 * @author Ravi Sharda
 */
public final class Log4jAuditLoggerFactory extends AuditLoggerFactory {
	
	private static volatile AuditLoggerFactory singletonInstance;
	
	//The Log4j logger factory to use
	LoggerFactory factory = new Log4jLoggerFactory();

    public static synchronized AuditLoggerFactory getInstance() {
        if ( singletonInstance == null ) {
             singletonInstance = new Log4jAuditLoggerFactory();
        }
        return singletonInstance;
    }
	
	protected Log4jAuditLoggerFactory() {}

	@Override
	public AuditLogger getLogger(String moduleName) {
		Log4jAuditLogger result = (Log4jAuditLogger)LogManager.getLogger(moduleName, factory);
		
		//logger.debug("result: " + result);
		
		//Layout layout = prepareLayout();
		//logger.debug("layout: " + layout);
		
		//addRollingFileAppender(result, layout);
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public AuditLogger getLogger(Class clazz) {
		return (AuditLogger)LogManager.getLogger(clazz.getName(), factory);
	}
	
}
